/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bynightServlet;

import bynightBusinessLogic.Gestore;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.util.Date;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Giuseppe
 */
public class ControllerServlet extends HttpServlet {
    @EJB
    private Gestore gestore;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String action=request.getParameter("action");
        if(action.equals("visualizzaRegistrazione")) {
           RequestDispatcher rd= getServletContext().getRequestDispatcher("/registrazione.jsp");
            rd.forward(request, response);
        } 
        else if(action.equals("registrazione"))
        {
            String user=(String) request.getParameter("scelta");
            if(user.equals("locale"))
            {
                String email = (String) request.getParameter("emailLocale");
                String indirizzo = (String) request.getParameter("indirizzoLocale");
                String nome = (String) request.getParameter("nomeLocale");
                String orario = (String) request.getParameter("orario");
                String numPosti =(String) request.getParameter("numPosti");
                String pIVA = (String) request.getParameter("PIVA");
                String proprietario = (String) request.getParameter("proprietario");
                String telefono = (String) request.getParameter("telefono");
                String username = (String) request.getParameter("username");
                String pass = (String) request.getParameter("password");
                
                gestore.registraLocale(nome, proprietario, indirizzo, telefono, pIVA, numPosti, orario, email, username, pass);
            }
            else
            {
                String email = (String) request.getParameter("email");
                String nome = (String) request.getParameter("nome");
                String cognome = (String) request.getParameter("cognome");
                String data = (String) request.getParameter("dataNascita");
                String indirizzo = (String) request.getParameter("indirizzo");
                String username = (String) request.getParameter("username");
                String telefono = (String) request.getParameter("telefonoC");
                String pass = (String) request.getParameter("password");
                
                gestore.registraCliente(nome, cognome, indirizzo, telefono, data, email, username, pass);
            }
        }
        else if(action.equals("cerca"))
        {
            String search = (String) request.getParameter("cerca");
            gestore.cerca(search);
        }
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ControllerServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ControllerServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
